package third.lib.wx.outpay.en;

import com.alibaba.fastjson.JSON;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.utils.StringUtils;
import third.lib.utils.XMLUtils;
import third.lib.wx.common.utils.WXUtils;
import third.lib.wx.outpay.en.config.WxOutPayEnConfig;
import third.lib.wx.outpay.en.model.WxOutPayEnRet;

//@Service
public class WxOutPayEn {
    private final Logger logger = LoggerFactory.getLogger(WxOutPayEn.class);
//    @Autowired
//    ThirdLogger thirdLogger;
//        @Autowired
//        WxOutPayEnConfig config;
//    WxOutPayEnConfig config;

    private String mchAppId;
    private String mchId;
    private String crtPath;
    private String key;


    public WxOutPayEn(String mchAppId, String mchId, String crtPath, String key){
        this.mchAppId = mchAppId;
        this.mchId = mchId;
        this.crtPath = crtPath;
        this.key = key;
    }

//    public WxOutPayEn(String mchAppId,
//                      String mchId,
//                      String crtPath,
//                      String key) {
//        config.setMchAppId(mchAppId);
//        config.setMchId(mchId);
//        config.setCrtPath(crtPath);
//        config.setKey(key);
//    }

    public WxOutPayEnRet sendEnPay(String nonceStr, String tradeNo, String openid, String amount, String desc) throws ThirdException {
//        thirdLogger.info("sendEnPay: " + JSON.toJSONString(this.config));
        SortedMap<String, Object> sortedMap = new TreeMap();
        sortedMap.put("mch_appid", this.mchAppId);
        sortedMap.put("mchid", this.mchId);
        sortedMap.put("nonce_str", nonceStr);
        sortedMap.put("partner_trade_no", tradeNo);
        sortedMap.put("openid", openid);
        sortedMap.put("check_name", "NO_CHECK");
        sortedMap.put("amount", amount);
        sortedMap.put("desc", desc);

        String sign;
        try {
            sign = WXUtils.sign(sortedMap, this.key);
        } catch (ThirdException var14) {
            var14.printStackTrace();
            throw var14;
        }

        String xmlStr = WXUtils.mapToStr(sortedMap, sign);
//        thirdLogger.info(xmlStr);
        String retStr;
        try {
            RestTemplate restTemplate = WXUtils.getRestTemplateWithCert(this.crtPath, this.mchId);
            retStr = (String) restTemplate.postForObject("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", xmlStr, String.class, new Object[0]);
        } catch (RestClientException var12) {
            var12.printStackTrace();
            throw new ThirdException(ThirdStatus.WX);
        } catch (ThirdException var13) {
            throw var13;
        }
//        thirdLogger.info(retStr);
        Map<String, Object> retMap = XMLUtils.prase(retStr);
        if (retMap == null) {
            throw new ThirdException(ThirdStatus.WX);
        } else {
            retMap = (Map) retMap.get("xml");
            if (!retMap.get("return_code").equals("SUCCESS")) {
                this.logger.error(retStr);
                throw new ThirdException(ThirdStatus.WX);
            } else if (!retMap.get("result_code").equals("SUCCESS")) {
                this.logger.error(retStr);
                throw new ThirdException(ThirdStatus.WX);
            } else {
                WxOutPayEnRet outPayRet = new WxOutPayEnRet();
                outPayRet.setMchAppId(this.mchAppId);
                outPayRet.setMchId(this.mchId);
                outPayRet.setPaymentNo(StringUtils.praseToStr(retMap.get("payment_no")));
                outPayRet.setPaymentTime(StringUtils.praseToStr(retMap.get("payment_time")));
                outPayRet.setTradeNo(StringUtils.praseToStr(retMap.get("partner_trade_no")));
                return outPayRet;
            }
        }
    }

}
